import '../public/index'
import url from '../data/url'
import request from '../data/request'
import { tableConfig } from '../data/config'
import store from 'store'

const base               = url.base,
      getPOSKeyboardPage = request.getPOSKeyboardPage

let table,
    storeData

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getPOSKeyboardPage,
  queryParams: function (params) {
    return $.extend({
      currentPage : params.pageNumber,
      pageSize    : params.pageSize},
      $('#searchForm').serializeObject())
  },
  columns: [
    {
      field: 'keySn',
      title: '键盘设备码',
      valign: 'middle'
    }, {
      field: 'remark',
      title: '备注',
      valign: 'middle'
    }, {
      field: 'rawAddTime',
      title: '添加时间',
      valign: 'middle',
      formatter: (index, row) => {
        return moment(index).format('YYYY-MM-DD HH:mm:ss')
      }
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      width: '200px',
      title: '操作',
      formatter: function(index, row) {
        return `<div class="btn-group">
                      <button type="button" class="btn btn-default btn-modify">修改</button>
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-bind">绑定商户</button>                   
                </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
})


// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage : 1,
    }, $('#searchForm').serializeObject())
  })
})

// 跳转绑定商户页面
$(document).on('click', '#table .btn-bind', (e) => {
  e.preventDefault()

  console.log('跳转绑定商户页面')

  let id = $(e.target).attr('data-id');

 window.location.href = './bind.html?keyboardId='+id;
})

//跳转至修改页
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')

  store.set('storeData', storeData)

  let id = $(e.target).attr('data-id')
  window.location.href = './modify.html'
})

